define("register", ["jquery", "md5", "jquery.validate", "jquery.idcode","jquery.cookie"], function($){
    return {
        init : function(){
            $(".checkd").change(function() {
                if($(".checkd").prop("checked")){
                    $(".reg-btn").attr('disabled',false);//按钮可用
                    $(".reg-btn").css("background-color","#e6133c");
                }
                else{
                    $(".reg-btn").css("background-color","#ccc");
                    $(".reg-btn").attr('disabled',true);//按钮不可用
                }
            });
            $.idcode.setCode();   //加载生成验证码方法
            $.validator.addMethod("checkUname", function(val, ele){
                var reg = /^[a-zA-Z]+/;
                return reg.test(val);
            }, "用户名不合法");
            $uname = $("[name=uname]").val();
            $("form").validate({
                rules : {
                    uname : {
                        required : true,
                        checkUname : true,
                        rangelength : [6, 16],
                        remote : {
                            url : "http://127.0.0.1/blecity/server/isExistUser.php",
                            type : "post",
                            data : {"unameA" : function(){return $("[name=uname]").val();}}
                        }
                    },
                    upwd : {
                        required : true,
                        rangelength : [6, 20]
                    },
                    upwd2 : {
                        equalTo : "#upwd"
                    },
                    uemail : {
                        required : true,
                        email : true
                    },
                    Txtidcode : {
                        required : true,
                    }
                },
                messages : {
                    uname : {
                        required : "用户名必填",
                        rangelength : "用户名必须是{0}到{1}之间",
                        remote : "该用户名已经存在!"
                    },
                    upwd : {
                        required : "密码必填",
                        rangelength : "密码必须是{0}到{1}之间"
                    },
                    upwd2 : {
                        equalTo : "2次密码不一致"
                    },
                    uemail : {
                        required : "邮箱必填",
                        email : "邮箱格式不合法"
                    },
                    Txtidcode : {
                        required : "",
                    }
                },
                //submitHandler
                submitHandler : function(){
                    var IsBy = $.idcode.validateCode();  //调用返回值，返回值结果为true或者false
                    if(IsBy){
                        var formdata = new FormData();
                        var $dataArr = $("form").serializeArray();
                        $dataArr.forEach((ele, index) =>{
                            if(ele.name != "Txtidcode" || ele.name != "upwd2"){
                                if(ele.name=="upwd"){
                                    formdata.append(ele.name,$.md5(ele.value));
                                }else{
                                    formdata.append(ele.name, ele.value);
                                }
                            }
                        });
                        $.ajax({
                            type : "post",
                            url : "./../server/register.php",
                            data : formdata,
                            processData:false,
                            contentType:false
                        }).then((res)=>{
                            if (res.status==1) {
                                $.cookie("islogin",`${$dataArr[0]["value"]},${$.md5($dataArr[1]["value"])}`,{ expires: 1/24/60/10 });
                                window.location.href="index.html";
                            }
                        })
                        return false;
                    } else {
                        alert("请重新输入")
                    }
                    return false;
                }
            })
        },
    }
})